<?php
// set_time_limit(0);
// //因为要间隔一定时间重新访问数据库，定义一个时间长度变量sleep_time
// ob_end_clean();//把数据从PHP的缓冲（buffer）中释放出来。
// ob_implicit_flush(1); //把不在缓冲（buffer）中的或者说是被释放出来的数据发送到浏览器。
// echo str_repeat(" ",1024);
// header('X-Accel-Buffering: no'); // 实时输出
// session_start();
$servername = "localhost";
$username = "root";
$password = "wgz52157";
$dbname = "myFirstDB";
// 创建连接
$conn = new mysqli($servername, $username, $password, $dbname);
// 检测连接
if ($conn->connect_error) {
    die("连接失败: " . $conn->connect_error);
}
$sessionid =  $_POST["sessionid"];
$round = $_POST["round"];
//定义轮数
$maxround = 4;
//定义每个组的人数
$maxgroupper=4;
//定义一个判断该用户是否已经在数据库的变量，如果在则为1，初始为0
 if ($round < $maxround) {
    $choosenow = $_POST["num"]; //从选择页面传入的用户本轮的选择值

    mysqli_query($conn, "UPDATE MyUsers SET choosenum='$choosenow',roundnum='$round',allincome=allincome-'$choosenow' WHERE id='$sessionid'");

    //获得该用户所在组的组数
    $groupnum = 0;
    $getgroupnum = mysqli_query($conn, "SELECT *FROM MyUsers WHERE id='$sessionid'");
    while ($group = mysqli_fetch_array($getgroupnum)) {
        $groupnum = $group['groupnum'];
    }


    //定义一个计算本轮分给每一个用户的值的收益变量cal
    $cal = '0';
    $sleep_time = 1;
    while (true) {
        $resultback = mysqli_query($conn, "SELECT * FROM MyUsers WHERE groupnum=$groupnum AND roundnum=$round ORDER BY usernamenum ASC");
        // $resultcount = $conn->query("SELECT COUNT(*) FROM MyUsers where groupnum=$insertgroupnum");
        //创建一个计数变量，判断本组是否所有用户均已完成选值
        $countbacknum = mysqli_num_rows($resultback);
        //开始对每一个用户的选择值进行判断及计算
        if ($countbacknum == $maxgroupper) {
            echo "第" . $round . "轮" . "<br>";
            while ($rowback = $resultback->fetch_assoc()) {
                echo $rowback["usernamenum"] . ":" . $rowback["choosenum"] . "<br>";
                $cal = $cal + $rowback["choosenum"];
            }
            $cal = ($cal * 1.5) / $maxgroupper;
            echo "收益:" . $cal;
            break;
        } else {
            sleep($sleep_time);
            continue;
        }
    }

    //得到收益后对数据库进行修改，将收益加入到allincome中
    mysqli_query($conn, "UPDATE MyUsers SET allincome=allincome+'$cal' WHERE id='$sessionid'");
    $getallincome = mysqli_query($conn, "SELECT *FROM MyUsers WHERE id='$sessionid'");
    $incomeout = 0;
    while ($group = mysqli_fetch_array($getallincome)) {
        $incomeout = $group['allincome'];
        break;
    }
} else {
    $choosenow = $_POST["num"]; //从选择页面传入的用户本轮的选择值

    mysqli_query($conn, "UPDATE MyUsers SET choosenum='$choosenow',roundnum='$round',allincome=allincome-'$choosenow' WHERE id='$sessionid'");

    //获得该用户所在组的组数
    $groupnum = 0;
    $getgroupnum = mysqli_query($conn, "SELECT *FROM MyUsers WHERE id='$sessionid'");
    while ($group = mysqli_fetch_array($getgroupnum)) {
        $groupnum = $group['groupnum'];
    }


    //定义一个计算本轮分给每一个用户的值的收益变量cal
    $cal = 0;
    $sleep_time = 1;
    while (true) {
        $resultback = mysqli_query($conn, "SELECT * FROM MyUsers WHERE groupnum=$groupnum AND roundnum=$round ORDER BY usernamenum ASC");
        // $resultcount = $conn->query("SELECT COUNT(*) FROM MyUsers where groupnum=$insertgroupnum");
        //创建一个计数变量，判断本组是否所有用户均已完成选值
        $countbacknum = mysqli_num_rows($resultback);
        //开始对每一个用户的选择值进行判断及计算
        if ($countbacknum == $maxgroupper) {
            echo "第" . $round . "轮" . "<br>";
            while ($rowback = $resultback->fetch_assoc()) {
                echo $rowback["usernamenum"] . ":" . $rowback["choosenum"] . "<br>";
                $cal = $cal + $rowback["choosenum"];
            }
            $cal = ($cal * 1.5) / $maxgroupper;
            echo "收益:" . $cal;
            break;
        } else {
            sleep($sleep_time);
            continue;
        }
    }
    mysqli_query($conn, "UPDATE MyUsers SET allincome=allincome+'$cal' WHERE id='$sessionid'");
    $getallincome = mysqli_query($conn, "SELECT *FROM MyUsers WHERE id='$sessionid'");
    $incomeout = 0;
    while ($group = mysqli_fetch_array($getallincome)) {
        $incomeout = $group['allincome'];
        break;
    }
}
?>
